package com.user_chat.chat.controller;

import com.user_chat.chat.bean.Login;
import com.user_chat.chat.bean.Register;
import com.user_chat.chat.bean.singlepage.UserInfor;
import com.user_chat.chat.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 获取用户信息
     * @param username
     * @return
     */
    @GetMapping("/getUserInfor")
    public UserInfor getUserInfor(@RequestParam("username")String username){
        return userService.getUserInfor(username);
    }


    /**
     * 该接口检查用户是否还存在
     * @param request
     * @return
     */
    @GetMapping("/tokenExists")
    public boolean tokenExists(HttpServletRequest request){
        return true;
    }


    /**
     * 退出
     * @param request
     * @return
     */
    @GetMapping("/exit")
    public boolean exit(HttpServletRequest request){
        return true;
    }

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public Login login(@RequestParam("username")String username,
                       @RequestParam("password")String password){
        Login login = userService.login(username, password);
        return login;
    }

    /**
     * 注册
     * @param username
     * @param password
     * @param authCode
     * @param phoneNumber
     * @return
     */
    @PostMapping("/register")
    public Register register(@RequestParam("username")String username,
                             @RequestParam(value = "password",required = false)String password,
                             @RequestParam(value = "authCode",required = false)String authCode, @RequestParam("phoneNumber")String phoneNumber){
        if(phoneNumber.equals("")){
            phoneNumber=null;
        }
        if(authCode.equals("")){
            authCode=null;
        }
        Register register = userService.register(username, password, authCode,phoneNumber);
        return register;
    }


}
